package com.membership.cloud.web;

import com.membership.cloud.exception.EmailExistException;
import com.membership.cloud.exception.InvalidItemException;
import com.membership.cloud.exception.NoInstanceException;
import com.membership.cloud.exception.PhoneExistException;
import com.membership.cloud.exception.ServerTechnicalException;
import com.membership.cloud.service.CustomerService;
import com.membership.cloud.web.model.CustomerPageResponse;
import com.membership.cloud.web.model.CustomerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by hiephn on 2014/07/23.
 */
@RequestMapping("/customer")
@RestController
public class CustomerController implements ControllerConstant {

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/filter/rest",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CustomerPageResponse filter(
            @RequestParam(value = "filterKey", required = false) String filterKey,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "page", required = false, defaultValue = DEFAULT_PAGE) int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = DEFAULT_PAGE_SIZE) int pageSize)
            throws ServerTechnicalException, InvalidItemException {
        CustomerPageResponse customerPageResponse = customerService.filter(page, pageSize, filterKey, birthday);
        return customerPageResponse;
    }

    @RequestMapping(value = "/rest",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CustomerPageResponse get(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "fullName", required = false) String fullName,
            @RequestParam(value = "barcode", required = false) String barcode,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "page", required = false, defaultValue = DEFAULT_PAGE) int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = DEFAULT_PAGE_SIZE) int pageSize)
            throws ServerTechnicalException, InvalidItemException {
        CustomerPageResponse customerPageResponse = customerService.
                get(page, pageSize, name, fullName, birthday, barcode, phone, email);
        return customerPageResponse;
    }

    @RequestMapping(value = "/rest",
        method = RequestMethod.PUT,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CustomerResponse create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "fullName", required = false) String fullName,
            @RequestParam(value = "barcode", required = false) String barcode,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "phone", required = false) String phone)
            throws InvalidItemException, PhoneExistException, EmailExistException {
        CustomerResponse customerResponse = customerService.
                create(name, fullName, birthday, barcode, phone, email);
        return customerResponse;
    }

    @RequestMapping(value = "/rest",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CustomerResponse update(
            @RequestParam(value = "id", required = false) int id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "fullName", required = false) String fullName,
            @RequestParam(value = "barcode", required = false) String barcode,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "phone", required = false) String phone)
            throws EmailExistException, InvalidItemException, NoInstanceException,
            PhoneExistException {
        CustomerResponse customerResponse = customerService.
                update(id, name, fullName, birthday, barcode, phone, email);
        return customerResponse;
    }

    @RequestMapping(value = "/rest",
        method = RequestMethod.DELETE,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String delete(int id) {
        customerService.delete(id);
        return OK_STATUS;
    }
}
